package com.cz.nongyetongv5.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.adapter.config.CommonConstants;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.utils.CharsetUtil;
import com.cz.nongyetongv5.utils.ConfigUtil;
import com.cz.nongyetongv5.utils.GoHomeUtil;

public class ShouYaoChaXunActivity extends Activity implements OnClickListener {

	private ViewGroup btnBack = null;

	private ViewGroup btnSearch1 = null;

	private ViewGroup btnSearch2 = null;

	private EditText edt1;
	private EditText edt2;

	@Override
	protected void onDestroy() {

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().remove(this);

		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().add(this);

		setContentView(R.layout.shouyao_chaxun);

		initView();

		initDate();

		initListener();

		loadRightTitle();
	}

	/**
	 * 加载去主页的按钮
	 */
	public void loadRightTitle() {

		ViewGroup btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		View view = GoHomeUtil.createPopView(getApplicationContext(),
				new String[] { "主页" });

		// 弹出更多的菜单
		final PopupWindow pop = new PopupWindow(view,
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, false);

		// 需要设置一下此参数，点击外边可消失
		pop.setBackgroundDrawable(new BitmapDrawable());

		// 设置点击窗口外边窗口消失
		pop.setOutsideTouchable(true);

		// 设置此参数获得焦点，否则无法点击
		pop.setFocusable(true);

		final Button btn01 = (Button) pop.getContentView().findViewWithTag(
				"btn0");

		OnClickListener menuOnClick = new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (v == btn01) {
					GoHomeUtil.goHome(getApplicationContext());
				}
			}
		};

		btn01.setOnClickListener(menuOnClick);

		btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		btnMeanMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (pop.isShowing()) {
					// 隐藏窗口，如果设置了点击窗口外小时即不需要此方式隐藏
					pop.dismiss();
				} else {
					// 显示窗口
					pop.showAsDropDown(v);
				}

			}
		});

	}

	private void initView() {

		btnBack = (ViewGroup) findViewById(R.id.btnBack);
		btnSearch1 = (ViewGroup) findViewById(R.id.btnSearch1);
		btnSearch2 = (ViewGroup) findViewById(R.id.btnSearch2);

		edt1 = (EditText) findViewById(R.id.edt1);
		edt2 = (EditText) findViewById(R.id.edt2);

	}

	private void initDate() {

	}

	private void initListener() {

		btnBack.setOnClickListener(this);
		btnSearch1.setOnClickListener(this);
		btnSearch2.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		if (btnBack == v) {

			onBackPressed();
		}

		if (btnSearch1 == v || btnSearch2 == v) {

			String searchText = "";

			String url = null;

			if (btnSearch1 == v) {

				searchText = edt1.getText().toString().trim();

				url = new ConfigUtil(getResources()).getPreHttpUrl()
						+ getString(R.string.touyongpin_shouyao_chaxun_url)
						+ searchText;

			} else if (btnSearch2 == v) {

				searchText = edt2.getText().toString().trim();

				url = new ConfigUtil(getResources()).getPreHttpUrl()
						+ getString(R.string.touyongpin_shouyao_qiyename_chaxun_url)
						+ searchText;
			}

			if (!CharsetUtil.isNotNull(searchText)) {

				Toast.makeText(ShouYaoChaXunActivity.this, "请输入要查询的内容..",
						Toast.LENGTH_SHORT).show();

				return;

			}

			Intent intent = new Intent(ShouYaoChaXunActivity.this,
					ShouYaoChaXunPageActivity.class);

			intent.putExtra("text", searchText);

			intent.putExtra("url", url);

			intent.putExtra("title", "兽药查询");

			startActivity(intent);

			// final CustomDialog customDialog = new CustomDialog(this);
			//
			// customDialog.setHeightPercent(0.35f);
			//
			// customDialog.setOnActionClickListener(new OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// customDialog.dismiss();
			//
			// }
			// });
			//
			// customDialog
			// .setNote("服务器正在搜集您所要查询的数据,由于搜集过程会持续一段时间,所以暂不开放此,敬请谅解,祝您工作愉快!");
			//
			// customDialog.setMessage("此功能暂不开放...");
			// customDialog.setActionTxt("确定");
			//
			// customDialog.show();

		}

	}
}
